/**
 * @filename:FasongyoujianServiceImpl Jan 16, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import com.wonder.app.domain.Fasongyoujian;
import com.wonder.app.mapper.FasongyoujianDao;
import com.wonder.app.service.FasongyoujianService;
import com.wonder.app.request.FasongyoujianRequest;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**   
 * @Description:TODO(发送邮件服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class FasongyoujianServiceImpl  extends ServiceImpl<FasongyoujianDao, Fasongyoujian> implements FasongyoujianService  {



	@Autowired
	private FasongyoujianService fasongyoujianService;


	@Autowired
	private FasongyoujianDao fasongyoujianDao;

    /**
     * 验证发送邮件信息
     * 
     * @param fasongyoujian
     * @param type
     * @return String
     */
    @Override
    public String validate(Fasongyoujian fasongyoujian,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:发送邮件分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Fasongyoujian>   //分页记录
	 */
	@Override 
	public IPage<Fasongyoujian> selectList(FasongyoujianRequest request, Page<Fasongyoujian> page){
	    LambdaQueryWrapper<Fasongyoujian> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(Fasongyoujian::getId, request.getId());
	      }               
             
        
		lambdaQueryWrapper.orderByDesc(Fasongyoujian::getId);	
		Page<Fasongyoujian> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }


	//发送邮件
	@Override
	//public boolean FaSong(Fasongyoujian entity) throws Exception {
	public boolean FaSong(Long id) throws Exception {

		Fasongyoujian fasongyoujian = fasongyoujianDao.selectById(id);

		//发件人的邮箱和密码（替换为自己的邮箱和密码）
		String user = fasongyoujian.getPromoterMail();//发件人邮箱
		String password = fasongyoujian.getAuthorizationCode();//授权码

		//发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般格式为: smtp.xxx.com
		//网易163邮箱的 SMTP 服务器地址为: smtp.163.com
		//String myEmailSMTPHost = "smtp.qq.com";
		String host = fasongyoujian.getMailServer();

		//收件人邮箱（替换为自己知道的有效邮箱）
		//String receiveMailAccount = "xxx@qq.com";
		String addressee = fasongyoujian.getRecipientEmail();
		///邮箱标题
		String title = fasongyoujian.getTitle();

		//内容
		String text = fasongyoujian.getContent();

		try {

			//邮件主题
			// String title = entity.getTheme();

			//创建Properties集合
			final Properties pros = new Properties();
			pros.put("mail.smtp.auth","true");          // 用户认证
			pros.put("mail.smtp.host",host);   // 主机名      这里是qq邮箱  如果是163  就把qq改为163
			// pros.put("mail.smtp.host","smtp.qq.com");   // 主机名      这里是qq邮箱  如果是163  就把qq改为163

			// 发件人的账号
			pros.put("mail.user",user);
			//pros.put("mail.user","2868886485@qq.com");
			// 发件人的授权码
			pros.put("mail.password",password);
			//pros.put("mail.password","wbcblsehxrvgddei");

			// 构建授权码信息  用于进行SMTP身份验证
			Authenticator authenticator = new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					//用户名 密码（授权码）
					String userName = pros.getProperty("mail.user");
					String password = pros.getProperty("mail.password");
					return new PasswordAuthentication(userName,password);
				}
			};

			// 创建邮件会话
			Session session = Session.getInstance(pros, authenticator);
			// 创建邮件信息
			MimeMessage message = new MimeMessage(session);
			// 创建发件人
			String name = pros.getProperty("mail.user");
			InternetAddress address = new InternetAddress(name);
			message.setFrom(address);

			// 设置收件人
			InternetAddress ToAddressee = new InternetAddress(addressee);
			//InternetAddress ToAddressee = new InternetAddress("2074832661@qq.com");
			message.setRecipient(Message.RecipientType.TO,ToAddressee);

			// 设置邮件标题
			message.setSubject(title);

			// 设置邮件内容
			message.setContent(text,"text/html;charset=UTF-8");

			// message.setContent("csshishuju","text/html;charset=UTF-8");

			// 发送邮件
			Transport.send(message);

			//charushuju(addressee , text , title ,  user );

			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}